 <template>
  <div class="area" v-if="cityInfo" ref="area_scroll">
    <div class="scroll_wrap">
      <div class="hot_wrap cityList">
        <div class="title">热门城市</div>
        <ul class="hot_city">
          <li
            @click="$emit('selectcity', item)"
            v-for="(item, index) in hot_city"
            :key="index"
          >
            {{ item }}
          </li>
        </ul>
      </div>
      <div class="city_wrap">
        <div
          class="city_content cityList"
          v-for="(item, index) in cityInfo.alphabet"
          :key="index"
        >
          <div class="title">{{ item }}</div>
          <ul>
            <li
              @click="$emit('selectcity', item.name)"
              v-for="(item, index) in cityInfo.cityList[index].cities"
              :key="index"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="area_keys">
      <ul>
        <li @click="selectKey(0)">#</li>
        <li
          @click="selectKey(index + 1)"
          v-for="(item, index) in cityInfo.alphabet"
          :key="index"
        >
          {{ item }}
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
export default {
  name: "Alphabet",
  props: {
    cityInfo: Object,
  },
  data() {
    return {
      hot_city: [
        "上海",
        "哈尔滨",
        "南京",
        "广州",
        "厦门",
        "杭州",
        "天津",
        "青岛",
      ],
      scroll: null,
    };
  },
  methods: {
    initScroll() {
      this.scroll = new BScroll(this.$refs.area_scroll, { click: true });
    },
    selectKey(index) {
      //找到所有包含cityList类的元素
      const cityList = this.$refs.area_scroll.getElementsByClassName(
        "cityList"
      );
      //根据下标滚动到对应的元素
      let el = cityList[index];
      //滚动到对应的位置，时间
      this.scroll.scrollToElement(el, 250);
    },
  },
};
</script>
<style scoped>
.area {
  margin-top: 10px;
  box-sizing: border-box;
  padding: 0 16px;
  background: #fff;
  height: calc(100% - 65px);
  overflow: hidden;
}
.scroll_wrap {
  background: #fff;
  overflow: auto;
}
.title {
  color: #aaa;
  padding: 15px 0;
}
.hot_city {
  padding: 0 16px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}
.hot_city li {
  width: 30%;
  background: #f1f1f1;
  margin: 0 10px 10px 0;
  text-align: center;
  padding: 10px;
  box-sizing: border-box;
}
.city_content li {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
.area_keys {
  position: fixed;
  right: 0;
  top: 25%;
  color: #aaa;
  font-size: 12px;
  line-height: 1.4;
  text-align: center;
  padding: 0 5px;
}
</style>